<?php
/**
 *
 * Author: Lyu
 * Date: 2023/2/2
 * Time: 18:25
 */

namespace App\Http\Controllers\Service;


use App\Http\Controllers\Controller;
use App\Library\Service\ExportService;
use Illuminate\Http\Request;
use function PHPUnit\Framework\at;

class CommonController extends Controller
{
    /**
     * 下载文件
     * @param Request $request
     * @return array|\Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function download(Request $request)
    {
        if (empty($request->input('file'))) {
            return alert_info(1, '文件不存在');
        }
        $filepath = ExportService::decryptFilePath($request->input('file'));
        if (empty($filepath)) {
            return alert_info(2, '文件不存在');
        }
        return response()->download($filepath);
    }


    /**
     * 下载文件
     * @param Request $request
     * @return array|\Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function downloadAndDelete(Request $request)
    {
        if (empty($request->input('file'))) {
            return alert_info(1, '文件不存在');
        }
        $filepath = ExportService::decryptFilePath($request->input('file'));
        if (empty($filepath)) {
            return alert_info(2, '文件不存在2');
        }
        $filepath = storage_path($filepath);
        if (!is_file($filepath)) {
            return alert_info(1, '文件不存在3');
        }
        return response()->download($filepath);
    }
}
